﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductRepair_ManageRepair_AutoBatchUnionforPackageProductSNs : System.Web.UI.Page
{
    DataTable dt;
    PackageProduct pp;
    PackageProductInfo ppInfo;

    public ProductRepair_ManageRepair_AutoBatchUnionforPackageProductSNs()
    {
        dt = new DataTable();
        pp = new PackageProduct();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Button5.Attributes.Add("onclick", "PasteData()");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string productSN = this.TextBox2.Text.Trim();
        if (productSN.Length < 10)
        {
            string str1 = "序列号过短，请检查！";
            //ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (productSN.Length > 100)
        {
            string str1 = "序列号过长，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (ProductSNCompare(productSN))
        {
            string str1 = "序列号已在列表中！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            this.ListBox1.Items.Add(productSN);
            this.Label1.Text = this.ListBox1.Items.Count.ToString();
            this.TextBox2.Text = "";
            this.TextBox2.Focus();
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        string str = this.myVal.Value;
        string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string st in strs)
        {
            if (!ProductSNCompare(st))
                this.ListBox1.Items.Add(st.Trim());
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected bool ProductSNCompare(string psn)
    {
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Text == psn)
            {
                return true;
            }
        }
        return false;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ListItemCollection lic = new ListItemCollection();
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Selected)
                lic.Add(item);
        }
        foreach (ListItem item in lic)
        {
            this.ListBox1.Items.Remove(item);
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Clear();
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        string pid = this.TextBox1.Text.Trim();
        string str1;
        ListItemCollection list = this.ListBox1.Items;
        if (!pp.IsvalidProductTypeIDtoUnion(pid, out str1))
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }
        if (list.Count == 0)
        {
            str1 = "请先添加序列号！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }
        if (!IsvalidPackageProductSNstoUnion(list, out str1))
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }
        BiuldDT(list);
        ppInfo = new PackageProductInfo("", "", pid, User.Identity.Name, DateTime.Now);
        if (pp.AutoBatchUnionforPackageProductSNs(ppInfo, dt))
            str1 = "成品序列号批量自动关联成功！";
        else
            str1 = "关联失败，请联系相关人员。";
        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        dt.Dispose();
    }
    
    //生成DataTable
    protected void BiuldDT(ListItemCollection list)
    {
        dt.Columns.Add("ProductSN");
        DataRow dr;
        foreach (ListItem item in list)
        {
            dr = dt.NewRow();
            dr["ProductSN"] = item.Text;
            dt.Rows.Add(dr);
        }
    }

    protected bool IsvalidPackageProductSNstoUnion(ListItemCollection list, out string str)
    {
        bool flag = true;
        str = "";
        foreach (ListItem item in list)
        {
            string str1;
            if (!pp.IsvalidPackageProductSNtoUnion(item.Text, out str1))
            {
                flag = false;
                str = str + item.Text + ",";
            }
        }
        if (flag)
        {
            return true;
        }
        else
        {
            str = "以下成品序列号已存在，或已经作为腔体序列号使用过，无法再使用：" + str + "请检查！";
            return false;
        }
    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void BindData()
    {
        string var_packagePrdouctSN = this.TextBox3.Text.Trim();
        IList<PackageProductInfo> list = new List<PackageProductInfo>();
        PackageProductInfo ppi = pp.GetPackageProductbyPackageProductSN(var_packagePrdouctSN);
        if (!string.IsNullOrEmpty(ppi.PackageProductSN))
        {
            list.Add(ppi);
        }
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
    }
}
